attach(mtcars)
plot(wt, mpg)
#lm函数能够做线性拟合
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
detach(mtcars)

#一个简单的例子
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
plot(dose, drugA, type="b")#type="b"表示连一个线
plot(dose, drugB, type="b")

#改变图形的样式 par
opar <- par(no.readonly = TRUE)
par(lty=2, pch=17)#改了个样式
plot(dose, drugB, type="b")

plot(dose, drugA, type = "b", lty=3, lwd=3, pch=1, cex=2)

#生成10个彩虹颜色
n <- 10
mycolors <- rainbow(n)
pie(rep(1, n), labels = mycolors, col = mycolors)#col是颜色的意思，labels表示显示标签
mygrays <- gray(0:n/n)#用10种灰度值显示
pie(rep(1, n), labels = mygrays, col = mygrays)

dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly = TRUE)
par(pin=c(2,3))#长2宽3
par(lwd=2, cex=1.5)#线条2倍，图标1.5倍
par(cex.axis=.75, font.axis=3)#刻度文字缩放倍数为0.75，字的大小为3
plot(dose, drugA, type="b", pch=19, lty=2, col="red")
plot(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green")
par(opar)

#文本标注
par(pin = c(4,3), mai = c(1, .5, 1, .2))
plot(dose, drugA, type="b",
     col="pink", lty=2, pch=2, lwd=2,
     main="Clinical Trials for DrugA", col.main = "yellow", #图片名称和名称的颜色
     sub="This is hypothetical data", col.sub = "orange", #下标题
     xlab="Dosage", ylab="Drug Response",
     xlim=c(0,60), ylim=c(0,70))

#坐标轴示例
x <- c(1:10)
y <- x
z <- 10/x
opar <- par(no.readonly = TRUE)
par(mar = c(5, 4, 4, 8) + 0.1)# mar增加边界大小
plot(x, y, type = "b",
     pch = 21, col = "red",
     yaxt = "n", lty = 3, ann = FALSE)
lines(x, z, type = "b", pch = 22, col = "blue", lty = 2)
axis(2, at = x, labels = x, col.axis = "red", las=2)
axis(4, at = z, labels = round(z, digits = 2),
     col.axis = "blue", las = 2, cex.axis = 0.7, tck = -.01)
mtext("y=1/x", side=4, line=3, cex.lab=1, las=2)
title("An Example of Creative Axes",
      xlab = "X values",
      ylab = "Y=X")
par(opar)


dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly = TRUE)
par(lwd=2, cex=1.5, font.lab=2)
plot(dose, drugA, type="b", 
     pch=15, lty=1, col="red", ylim=c(0,60),
     main="Drug A vs. Drug B",
     xlab="Drug Dosage", ylab="Drug Response")
lines(dose, drugB, type="b",
      pch=17, lty=2, col="blue")
abline(h=c(30), lwd=1.5, lty=2, col="gray")
#显示图例
library(Hmisc)
minor.tick(nx=3, ny=3, tick.ratio = 0.5)
legend("topleft", inset = .05, title = "Drug Type", c("A", "B"),
       lty=c(1,2), pch = c(15, 17), col=c("red", "blue"))
par(opar)

#点图
attach(mtcars)
plot(wt, mpg,
     main="Mileage vs. Car Weight",
     xlab="Weight", ylab="Mileage",
     pch=18, col="blue")
text(wt, mpg,
     row.names(mtcars),
     cex=0.6, pos=4, col="red")
detach(mtcars)

#条形图
#install.packages("vcd")
library(vcd)
counts <- table(Arthritis$Improved)
#简单条形图
barplot(counts, 
        main="Simple Bar Plot",
        xlab="Improvement", ylab="Frequency")
#水平条形图
barplot(counts,
        main="Horizon Bar Plot",
        xlab="Frequency", ylab="Improvement",
        horiz = TRUE)
#堆砌条形图
counts <- table(Arthritis$Improved, Arthritis$Treatment)
barplot(counts,
        main="Stacked Bar Plot",
        xlab="Treatment", ylab="Frequency",
        col=c("red", "yellow", "green"),
              legend=rownames(counts))

#分组条形图
barplot(counts,
        main="Grouped Bar Plot",
        xlab="Treatment", ylab="Frequency",
        col=c("red", "yellow", "green"),
        legend=rownames(counts), beside=TRUE)

#均值条形图
states <- data.frame(state.region, state.x77)
means <- aggregate(states$Illiteracy, by=list(state.region), FUN=mean)
means <- means[order(means$x),]#排序
barplot(means$x, names.arg = means$Group.1)
title("Mean Illireracy Rate")

#改成横向
par(mar=c(5,8,4,2))
par(las=2)
counts <- table(Arthritis$Improved)
barplot(counts,
        main="Treatment Outcome",
        horiz = TRUE, cex.names = 0.8,
        names.arg = c("No Improvemwnt", 
        "Some Improvement", "Marked Improvement"))

#棘状图
library(vcd)
attach(Arthritis)
counts <- table(Treatment, Improved)
spine(counts, main="Spinogram Example")
detach(Arthritis)

#直方图
hist(mtcars$mpg,
     breaks = 24,
     col = "skyblue",
     xlab = "Miles Per Gallon",
     main = "Colored histogram with 24 bins")

#增加细节
hist(mtcars$mpg,
     freq = FALSE,
     breaks = 12,#分成12块
     col = "skyblue",
     xlab = "Miles Per Gallon",
     main = "Colored histogram with 24 bins")
rug(jitter(mtcars$mpg))
lines(density(mtcars$mpg), col="blue", lwd=2)

#扇形图
library(plotrix)
slices <- c(10, 12, 4, 16, 8)
lbls <- c("uS", "UK", "Australia", "Germany", "France")
fan.plot(slices, labels = lbls, main="Fan Plot")

#核密度图
par(mfrow=c(2,1))
d <- density(mtcars$mpg)
plot(d, main="Kernel Density of Miles Per Gallon")
polygon(d, col="red", border = "blue")#上一点色彩

#箱线图
boxplot(mtcars$mpg, main="Box plot", 
        ylab="Miles Per Gallon")
#展示多个  
boxplot(mpg ~ cyl, data = mtcars,
        main="Car Milleage Data",
        xlab="Number of Cylinders",
        ylab="Miles Per Gallon")
#换个样式
boxplot(mpg ~ cyl, data=mtcars,
        notch=TRUE,#获得凹槽
        varwidth=TRUE,
        col="red",
        main="Car Milleage Data",
        xlab="Number of Cylinders",
        yalb="Miles Per Gallon")
#两个交叉因子的箱线图
mtcars$cyl.f <- factor(mtcars$cyl,
                       levels=c(4,6,8),
                       labels=c("4", "6", "8"))
mtcars$am.f <- factor(mtcars$am,
                      levels = c(0,1),
                      labels = c("auto","standard"))
barplot(mpg~am.f*cyl.f,
        data=mtcars,
        varwidth=TRUE,
        col=c("gold", "darkgreen"),
        main="MPG Distrubution by Auto Type",
        xlab="Auto Type")

#点图
dotchart(mtcars$mpg, labels=row.names(mtcars),
         main="Gas Milleage for Car Models",
         xlab="Miles Per Gallon")

#图形的组合

